<template>
	<view>
		<div class='content' v-show="form === 1">
			<u--form
				labelPosition="left"
				:model="model1"
				ref="form1"
				labelWidth='24'
				:rules="rules1"
			>
				<u-form-item
					prop="userInfo1.phone"
					borderBottom
					leftIcon='phone'
					leftIconStyle='font-size:20px'
					ref="item1">
					<u--input
						v-model="model1.userInfo1.phone"
						border="none"  placeholder="请输入您的手机号" maxlength="11">
					</u--input>
				</u-form-item>
				<u-form-item
					prop="userInfo1.password"
					borderBottom
					leftIcon='lock'
					leftIconStyle='font-size:20px'
					ref="item1">
					<u--input
						v-model="model1.userInfo1.password"
						border="none"  placeholder="请输入您的密码" maxlength='6'>
					</u--input>
				</u-form-item>
			</u--form>
		</div>
		<div class='content' v-show="form === 2">
			<u--form
				labelPosition="left"
				:model="model2"
				ref="form1"
				labelWidth='24'
				:rules="rules2"
			>
				<u-form-item
					prop="userInfo2.phone"
					borderBottom
					leftIcon='phone'
					leftIconStyle='font-size:20px'
					ref="item1">
					<u--input
						v-model="model2.userInfo2.phone"
						border="none"  placeholder="请输入您的手机号"  maxlength="11">
					</u--input>
					<u-button
						slot="right"
						@tap="getCode"
						:text="tips"
						type="success"
						size="mini"
						:disabled="disabled1"
					></u-button>
				</u-form-item>
				<u-form-item
					prop="userInfo.verificationCode"
					borderBottom
					ref="item2">
					<u--input
						v-model="model2.userInfo2.verificationCode"
						border="none"  placeholder="请输入您收到的验证码">
					</u--input>
				</u-form-item>
			</u--form>
		</div>
		<div class='BTN'>
			<div v-show='form === 1'>
				<u-button
					text="登录" 
					shape='circle' 
					color="linear-gradient(to right, rgb(46, 216, 196), rgb(54, 220, 170))" 
					@click='login1'>
				</u-button>
			</div>
			<div v-show="form === 2">
				<u-button
					text="登录" 
					shape='circle' 
					color="linear-gradient(to right, rgb(46, 216, 196), rgb(54, 220, 170))" 
					@click='login2'>
				</u-button>
			</div>
			<div class='flex'>
				<p v-show="form === 1" @click='codeTo'>通过短信验证码登录</p>
				<p v-show="form === 2" @click='passwordTo'>通过密码登录</p>
			</div>
		</div>
		<u-code
			ref="uCode"
			@change="codeChange"
			seconds="20"
			@start="disabled1 = true"
			@end="disabled1 = false"
		></u-code>
	</view>
</template>

<script>
	import { mapState } from 'vuex'//引入mapState
	export default {
		data() {
			return {
				form: 1,	
				LOGTIME:null,
				model1:{
					userInfo1: {
						phone: null,
						password: null,
					},
				},
				model2:{
					userInfo2:{
						phone:'',
						verificationCode:''
					},
				},
				tips: '获取验证码',
				disabled1: false,
				rules1: {
					'userInfo1.password': {
						type: 'any',				
						required: true,
						message: '请填写密码',
						trigger: ['change','blur'],
					},
					'userInfo1.phone': [
						{
							type: 'any',
							required: true, 
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不合法',
							// 触发器可以同时用blur和change
							trigger: ['change','blur'],
						}
					]
				},
				rules2: {
					'userInfo2.phone': [
						{
							type: 'any',
							required: true, 
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change','blur'],
						}
					]
				},
			};
		},
		methods: {
			login1(){
				if(this.model1.userInfo1.phone && this.model1.userInfo1.password){
					this.$api.DLQlogin({
						phone:this.model1.userInfo1.phone,
						password:this.model1.userInfo1.password,
					}).then((res)=>{
						console.log(res)
						if(res.code === 200){
							uni.setStorageSync("token",res.data.token)
							this.$api.DLQgetUserInfo().then((res)=>{
								console.log(res)
								uni.setStorageSync('userInfo', res.data)
								this.getlogTime()
								uni.setStorageSync('logTime', this.LOGTIME)
							})
							uni.switchTab({
								url:'/pages/Home/Home'
							})
						}else{
							uni.showToast({
								icon:'error',
								title:'账号不存在',
								duration:2000,
							})
							this.model1.userInfo1.phone = null
							this.model1.userInfo1.password = null
						}
					})
				}else{
					uni.showToast({
						icon:'error',
						title:'请输入'
					})
				}
				// console.log(1)
			},
			login2(){
				// this.$api.DLQ.
				console.log(2)
			},
			getlogTime(){
				this.LOGTIME = Date.now();
			},
			codeTo(){
				this.form = 2
			},
			passwordTo(){
				this.form = 1
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码为：123456');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			
		},
	}
</script>

<style lang="scss" scoped>
	.content{
		border-radius: 26upx;
		padding: 30upx 40upx;
		margin: 36upx;
		box-shadow: 0 0 5px rgba(0,0,0,.1);
	}
	.BTN{
		margin: 36upx;
		.flex{
			display: flex;
			justify-content: flex-end;
			p{
				text-align: right;
				font-size: 28upx;
				color: #2ED8C4;
				margin:20upx;
				font-weight: 600;
			}
		}
		
	}
</style>